<?php

/**
 * DynamicPageController is the default controller for this application
 */
class DynamicPageController extends BrowseControllerBase {
	/*
	 * UserPageDataService Object
	 */
	public $userPageDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->requireUserLogin ();
		
		/*
		//Check if User is logged in
		$authentication = new Authentication();
		if ($authentication->getCurrentUserId() == null) {
			// Go on to login page
			$url = Zend_Registry::get('config')->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect($url);
			return -1;
		}
		*/
		
		// Init all DataService necessary
		$this->userPageDataService = new UserPageDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$authentication = new Authentication ();
		
		$arrElement = array ();
		$arrElement ['ownerId'] = $authentication->getCurrentUserId ();
		// Dynamic Page only
		$arrElement ['type'] = PAGE_CUSTOM;
		
		$numRecordPerPage = 10;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		$totals = $this->userPageDataService->getTotals ( $arrElement );
		$itemList = $this->userPageDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Browse/DynamicPage/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'pageEntities', $itemList );
		$this->smarty->assign ( 'pageCount', count ( $itemList ) );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/UserPage/dynamic_page_list.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function createAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// Only Normal User and Admin can add dynamic page
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// get content
			$content = $this->getPost ( 'content', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$content = stripslashes ( $content );
			}
			
			$content = trim ( $content );
			
			// head title
			$headTitle = $this->getPost ( 'headTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Main Title
			$title = $this->getPost ( 'title', "" );
			
			$status = UserPageEntity::ACTIVE;
			
			// check
			if (! empty ( $name ) && ! empty ( $content )) {
				$status = intval ( $status );
				$authentication = new Authentication ();
				try {
					// Save data into database
					$pageEntity = $this->userPageDataService->beforeCreate ();
					$pageEntity->name = $name;
					$pageEntity->content = $content;
					$pageEntity->headTitle = $headTitle;
					$pageEntity->status = $status;
					$pageEntity->metaDescription = $metaDescription;
					$pageEntity->metaKeywords = $metaKeywords;
					$pageEntity->title = $title;
					$pageEntity->ownerId = $authentication->getCurrentUserId ();
					$pageEntity->type = PAGE_CUSTOM;
					
					$this->userPageDataService->create ( $pageEntity );
					
					// Redirect to edit page
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/DynamicPage/index';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/UserPage/create_page.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function editAction() {
		// Error flag
		$isError = false;
		$error = "";
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of page does not exist, go on to view list page
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->indexAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// Get data
		$pageEntity = $this->userPageDataService->getById ( $id );
		
		// check data
		if (empty ( $pageEntity )) {
			$this->indexAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// Only Normal user and admin can edit this content
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// get content
			$content = $this->getPost ( 'content', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$content = stripslashes ( $content );
			}
			
			$content = trim ( $content );
			$content = htmlentities ( $content, ENT_QUOTES );
			
			// head title
			$headTitle = $this->getPost ( 'headTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Main Title Page
			$title = $this->getPost ( 'title', "" );
			
			$pageEntity->name = $name;
			$pageEntity->content = $content;
			$pageEntity->headTitle = $headTitle;
			$pageEntity->metaDescription = $metaDescription;
			$pageEntity->metaKeywords = $metaKeywords;
			$pageEntity->title = $title;
			
			// check
			if (! empty ( $name ) && ! empty ( $content )) {
				try {
					$res = $this->userPageDataService->update ( $pageEntity );
					
					$isError = false;
					
					// Show a message
					$error = 'The record has been updated successfully';
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'id', $pageEntity->id );
		$this->smarty->assign ( 'name', $pageEntity->name );
		$this->smarty->assign ( 'content', $pageEntity->content );
		$this->smarty->assign ( 'headTitle', $pageEntity->headTitle );
		$this->smarty->assign ( 'title', $pageEntity->title );
		$this->smarty->assign ( 'metaDescription', $pageEntity->metaDescription );
		$this->smarty->assign ( 'metaKeywords', $pageEntity->metaKeywords );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/UserPage/edit_page.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Delete Dynamic Page
	 */
	public function delAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		
		// Onyly normal user and admin can delete this content
		if (! empty ( $id ) && is_numeric ( $id ) && $this->isDemoMode == false) {
			
			$id = intval ( $id );
			// Delete page
			$res = $this->userPageDataService->del ( $id );
		}
		// Redirect to edit page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/DynamicPage/index';
		CommonUtils::redirect ( $url );
		return;
	}
}
